﻿using SHKF.B2C.Model.Dto.Center;
using SHKF.B2C.Model.Entity.Center;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SHKF.B2C.Mannage.AdminUI.View.Set.Server
{
    /// <summary>
    /// frmServer.xaml 的交互逻辑
    /// </summary>
    public partial class frmServer : Page
    {
        public frmServer()
        {
            InitializeComponent();
        }


        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            LoadServer();

        }

        private void btnServerAdd_Click(object sender, RoutedEventArgs e)
        {
            frmServerEdit frmServer = new frmServerEdit();
            frmServer.DataContext = new ErpServerEntity();
            if( frmServer.ShowDialog() == true )
            {
                this.LoadServer();
            }
        }

        private void btnServerEdit_Click(object sender, RoutedEventArgs e)
        {
            if( this.dgvServer.SelectedItem != null)
            {
                ErpServerEntity serverEntity = (ErpServerEntity)this.dgvServer.SelectedItem;
                frmServerEdit frmServer = new frmServerEdit();
                frmServer.DataContext = serverEntity.Clone();
                if (frmServer.ShowDialog() == true)
                {
                    this.LoadServer();
                }
            }
           
        }
 
        private void btnServerSearch_Click(object sender, RoutedEventArgs e)
        {
            LoadServer();
        }

        private async void LoadServer()
        {
            ErpServerListDto dto = await GL.HttpClientManage.QueryErpServer();
            if(dto.error == null)
            {
                this.dgvServer.ItemsSource = dto.data;
            }
            else
            {
                MessageBox.Show(dto.error.msg);
            }
        }

        private void btnDataBaseAdd_Click(object sender, RoutedEventArgs e)
        {
            ErpServerEntity server = this.SelectedServer;
            if( server != null)
            {
                frmDataBaseEdit win = new frmDataBaseEdit();
                win.DataContext = new ErpDataBaseEntity() { FKSN = (int)server.SN };
                if (win.ShowDialog() == true)
                {
                    this.LoadDataBase();
                } 
            }
            else
            {
                MessageBox.Show("请选择服务器");
            }
        }

        private void btnDataBaseEdit_Click(object sender, RoutedEventArgs e)
        {
            if (this.dgvDataBase.SelectedItem != null)
            {
                ErpDataBaseEntity data = (ErpDataBaseEntity)this.dgvDataBase.SelectedItem;
                frmDataBaseEdit win = new frmDataBaseEdit();
                win.DataContext = data.Clone();
                if (win.ShowDialog() == true)
                {
                    this.LoadDataBase();
                }
            }
        }

        private void btnDataBaseSearch_Click(object sender, RoutedEventArgs e)
        {
            LoadDataBase(); 
        }

        private async void LoadDataBase()
        { 
            int fksn = 0;
            ErpServerEntity server = SelectedServer;
            if (server != null)
            {
                fksn = (int)server.SN;
                ErpDataBaseListDto dto = await GL.HttpClientManage.QueryErpDataBase(fksn);
                if (dto.error == null)
                {
                    this.dgvDataBase.ItemsSource = dto.data;
                }
                else
                {
                    MessageBox.Show(dto.error.msg);
                }
            }
            
        }

        private ErpServerEntity SelectedServer
        {
            get
            {
                if( this.dgvServer.SelectedItem != null)
                {
                    return (ErpServerEntity)this.dgvServer.SelectedItem;
                }
                return null;
            }
        }
    }
}
